#include "stdafx.hpp"
#include "ColligereDatabaseTreeCtrlBase.hpp"
#include "ColligereUtils.hpp"

IMPLEMENT_ABSTRACT_CLASS ( col::DatabaseTreeCtrlBase, wxTreeCtrl );

col::DatabaseTreeCtrlBase::DatabaseTreeCtrlBase()
{
}

col::DatabaseTreeCtrlBase::~DatabaseTreeCtrlBase()
{
}

void col::DatabaseTreeCtrlBase::SetBrowseBy ( wxUint32 browseBy ) 
{ 
	mBrowseBy = browseBy; 
}

wxUint32 col::DatabaseTreeCtrlBase::GetBrowseBy() const 
{ 
	return mBrowseBy; 
}

wxTreeItemId col::DatabaseTreeCtrlBase::FindItem ( wxTreeItemId root, 
												  const wxString &rText )
{
	wxASSERT ( root.IsOk() );

	boost::scoped_ptr < wxtreectrl_tree_adapter <> > ta 
		( new wxtreectrl_tree_adapter <> ( this ) );

	return find_first_child_node < wxTreeItemId > ( rText, root, *ta );
}

col::TreeItemData *col::DatabaseTreeCtrlBase::GetSelectedItemData()
{
	const wxTreeItemId selection ( GetSelection() );
	wxASSERT ( selection.IsOk() );
	return dynamic_cast < TreeItemData * > ( GetItemData ( selection ) );
}

void col::DatabaseTreeCtrlBase::SortCollection ( const wxTreeItemId &rRoot ) 
{
	const wxTreeItemId root = rRoot.IsOk() ? rRoot : GetRootItem();

	boost::scoped_ptr < wxtreectrl_tree_adapter <> > ta 
		( new  wxtreectrl_tree_adapter <> ( this ) );

	traverse < wxTreeItemId > ( root, *ta, SortChildrenAsc ( this ) );
}